﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; 


namespace HOOMT
{
    public partial class MethodsDialog : Form
    {
        //“临时”方法列表，根据添加方法对话框实时更新方法对话框的显示，（显示更新)
        //方法对话框返回 DialogResult.OK 时，用临时方法列表更新对象的方法列表（数据更新）
        private List<Method> temporaryMethodList = new List<Method>();
        private int myLine, offSet;//记录选中行号，选中行偏移(行起始位置)
        private bool hasSelectedItem = false;
        //int offSet = -1;
        //####################################################################################
        public MethodsDialog()
        {
            InitializeComponent();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
        }
        
        //************************************************************************************
        public RichTextBox MethodShowBox
        {
            get
            {
                return methodsShowBox;
            }
            set
            {
                methodsShowBox = value;
            }
        }
        public List<Method> TemporaryMethodList
        {
            get
            {
                return temporaryMethodList;
            }
            set
            {
                temporaryMethodList = value;
            }
        }
        private void MethodsDialog_Load(object sender, EventArgs e)
        {

        }
        //************************************************************************************
        //鼠标按下响应
        [DllImport( "user32.dll ")]//要搞清楚其原理
        public   static   extern   System.Int32   HideCaret(System.IntPtr   hwnd);
        private void methodsShowBox_MouseDown(object sender, MouseEventArgs e)
        {
            //选中Item
            if (methodsShowBox.Text.Length != 0)
            {
                int myStart = this.methodsShowBox.SelectionStart;//鼠标点击位置
                myLine = this.methodsShowBox.GetLineFromCharIndex(myStart);//获取选中行号(从0开始)
                if (methodsShowBox.Lines[myLine].Length > 0)
                {
                //计算选中的行的开始位置
                offSet = 0;
                for (int i = 0; i < myLine; i++)
                {
                    offSet += methodsShowBox.Lines[i].Length;
                    offSet += 1;//"\n"
                }
                //选中行
                methodsShowBox.Select(offSet, methodsShowBox.Lines[myLine].Length);
                hasSelectedItem = true;//置位
                }
                else
                    hasSelectedItem = false;
            }
            else
                hasSelectedItem = false;
            HideCaret(((RichTextBox)sender).Handle);//隐藏光标
        }
        //************************************************************************************
        //在添加方法对话框中添加方法，并更新方法对话框的显示
        private void addButton_Click(object sender, EventArgs e)
        {
            MethodAddDialog addForm = new MethodAddDialog();
            if (addForm.ShowDialog() == DialogResult.OK)
            {
                if (!String.IsNullOrWhiteSpace(addForm.ReturnType) && !String.IsNullOrWhiteSpace(addForm.MethodName))
                {
                    //要求去除前后空格
                    Method method = new Method(addForm.AccessModifier.Trim(), addForm.ReturnType.Trim(), 
                        addForm.MethodName.Trim(), addForm.ParameterList.Trim(), addForm.Description.Trim());
                    this.MethodShowBox.Text += method.ToString();
                    this.MethodShowBox.Text += "\n";
                    this.TemporaryMethodList.Add(method);
                    //    XmlHelper xmlTool = XmlHelper.GetInstance();
                    //   xmlTool.AddMethod(method);
                }
                else
                {
                    if (String.IsNullOrWhiteSpace(addForm.ReturnType))
                        MessageBox.Show("Invalid Inputs，ReturnType is null!");
                    else
                        MessageBox.Show("Invalid Inputs，MethodName is null!");
                }
            }
            hasSelectedItem = false;//复位
        }
        //************************************************************************************
        //删除方法
        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (hasSelectedItem)//在有选中条件下操作
            {
                StringBuilder str1 = new StringBuilder();
                str1.Append(methodsShowBox.Text);
                str1.Remove(offSet, methodsShowBox.Lines[myLine].Length + 1);
                methodsShowBox.Text = str1.ToString();
                temporaryMethodList.RemoveAt(myLine);
                Invalidate();
                hasSelectedItem = false;//复位
            }

        }
        //************************************************************************************
        //修改方法
        private void modifyButton_Click(object sender, EventArgs e)
        {
            if (hasSelectedItem)//在有选中条件下操作
            {
                String str = methodsShowBox.Lines[myLine];
                MethodAddDialog modifyForm= new MethodAddDialog();
                modifyForm.Text = "MethodModifyDialog";
                //设置方法对话框的初始显示值
                String[] arrstr;
                arrstr = str.Split();
                modifyForm.AccessModifier = arrstr[0];
                modifyForm.ReturnType = arrstr[2];//因为中间有两个空格
                //modifyForm.Description = arrstr[6];
                String nameAndParameter = str.Substring(arrstr[0].Length + arrstr[2].Length + 4);//4为空格数
                int parameretStart = 0, parameterLength;
                for (int i = 0; i < nameAndParameter.Length; i++)
                {
                    if (nameAndParameter[i].Equals('('))
                    {
                        modifyForm.MethodName = nameAndParameter.Substring(0, i);
                        parameretStart = i + 1;
                        //modifyForm.ParameterList = nameAndParameter.Substring(i + 1, nameAndParameter.Length - i - 3);//
                        //break;
                    }
                    else if (nameAndParameter[i].Equals(')'))
                    {
                        parameterLength = i - parameretStart;
                        modifyForm.ParameterList = nameAndParameter.Substring(parameretStart, parameterLength);
                        modifyForm.Description = nameAndParameter.Substring(i + 4);
                        break;
                    }
                }
                //更新修改后的显示和temporaryMethodList列表
                if (modifyForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (!String.IsNullOrWhiteSpace(modifyForm.ReturnType) && !String.IsNullOrWhiteSpace(modifyForm.MethodName))
                    {
                        Method method = new Method(modifyForm.AccessModifier.Trim(), modifyForm.ReturnType.Trim(), 
                            modifyForm.MethodName.Trim(), modifyForm.ParameterList.Trim(), modifyForm.Description.Trim());
                        //更新显示
                        StringBuilder str2 = new StringBuilder();
                        str2.Append(methodsShowBox.Text);
                        str2.Replace(str, method.ToString());
                        methodsShowBox.Text = str2.ToString();
                        //更新临时列表
                        temporaryMethodList[myLine] = method;
                    }
                    //Invalidate();
                }
                hasSelectedItem = false;//复位
            }
        }
        //************************************************************************************
        

    }
}
